package com.miaxis.livedetection;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.miaxis.image.MXImage;
import com.miaxis.image.MXImages;
import com.miaxis.livedetect.DetectResult;
import com.miaxis.livedetection.test.R;

import java.util.Objects;

import timber.log.Timber;

/**
 * @date: 2018/12/4 11:40
 * @author: zhang.yw
 * @project: LiveDetection
 */
public class ResultActivity extends AppCompatActivity {

    public static final String KEY_RESULT = "key_result";

    ImageView imageView;
    TextView textViewResult;
    TextView textViewReason;
    TextView textViewAction;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        setSupportActionBar(findViewById(R.id.toolbar));
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        imageView = findViewById(R.id.imageView);
        textViewResult = findViewById(R.id.textViewResult);
        textViewReason = findViewById(R.id.textViewReason);
        textViewAction = findViewById(R.id.textViewAction);

        DetectResult detectResult = getIntent().getParcelableExtra(KEY_RESULT);
        if (null != detectResult) {
            showDetectResult(detectResult);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.retry, menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void showDetectResult(DetectResult detectResult) {
        if (detectResult.code == 0) {
            textViewResult.setText("活体");
            textViewReason.setVisibility(View.GONE);
            textViewAction.setVisibility(View.GONE);
            findViewById(R.id.tempReason).setVisibility(View.GONE);
            findViewById(R.id.tempAction).setVisibility(View.GONE);
        } else {
            textViewResult.setText("非活体");
            switch (detectResult.failReason) {
                case DetectResult.ERROR_BRIGHT_HIGH:
                    textViewReason.setText("亮度高");
                    break;
                case DetectResult.ERROR_BRIGHT_LOW:
                    textViewReason.setText("亮度低");
                    break;
                case DetectResult.ERROR_MORE_FACE:
                    textViewReason.setText("多张人脸");
                    break;
                case DetectResult.ERROR_NO_FACE:
                    textViewReason.setText("未检测到人脸\n或检测时离开");
                    break;
                case DetectResult.ERROR_TIMEOUT:
                    textViewReason.setText("动作超时");
                    break;
                case DetectResult.ERROR_DISTANCE:
                    textViewReason.setText("距离屏幕过远");
                    break;
                case DetectResult.ERROR_PITCH:
                    textViewReason.setText("人脸过暗");
                    break;
                case DetectResult.ERROR_SYMMETRY:
                    textViewReason.setText("未对正摄像头");
                    break;
                default:
                    textViewReason.setText("UnKnow,Code=" + detectResult.code);
                    break;
            }
            String actionText;
            switch (detectResult.failAction) {
                case DetectResult.ACTION_BRIGHTNESS:
                    actionText = "亮度评测";
                    break;
                case DetectResult.ACTION_GAZY:
                    actionText = "姿态评测";
                    break;
                case DetectResult.ACTION_BLINK:
                    actionText = "眨眼检测";
                    break;
                case DetectResult.ACTION_MONTH:
                    actionText = "张嘴检测";
                    break;
                case DetectResult.ACTION_NOD:
                    actionText = "点头检测";
                    break;
                case DetectResult.ACTION_SHAKING:
                    actionText = "摇头检测";
                    break;
                default:
                    actionText = "UnKnow,Code=" + detectResult.failAction;

            }
            textViewAction.setText(actionText);
        }
        showImage(detectResult.image);
    }

    private void showImage(final MXImage image) {
        Timber.w("ShowImage %s ", image);
        if (image == null || image.getData() == null || image.getData().length == 0) {
            imageView.setVisibility(View.GONE);
            return;
        }
        final Bitmap bitmap = MXImages.covertToBitmap(image);
        imageView.setImageBitmap(bitmap);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                setResult(RESULT_CANCELED);
                finish();
                return true;
            case R.id.retry:
                setResult(RESULT_OK, null);
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
